package lc.位运算;

/**
 * @description:
 * @author: qian
 * @createDate: 2021/10/29
 */
public class 剑指Offer16_数值的整数次方 {

    // 快速幕
    // 2^11 = 1 0 1 1
    //      = 2^8 + 2^4 + 2^0
    public double myPow(double x, int n) {

        double result = 1;
        long N = Math.abs((long)n);
        while (N != 0) {
            if ((N & 1) == 1) result *= x;
            x *= x;
            N >>= 1;
        }

        return n < 0 ? 1/result : result;
    }
}
